package pw.archipelag.loaderGame.bg {
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.Event;
	
	public class Bg extends Sprite {
		
		[Embed(source="../!embed/assetsLoaderGame.swf", symbol="logoAnim")]
		public static const classLogoAnim: Class;
		
		private var sprLogoAnim: Sprite;
		private var shapeBg: Shape;
		
		public function Bg(): void {
			this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
		}
		
		protected function onAddedToStage(e: Event): void {
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
			this.addShapeBg();
			this.addLogoAnim();
			this.stage.addEventListener(Event.RESIZE, this.onStageResize);
			this.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		private function addShapeBg(): void {
			this.shapeBg = new Shape();
			this.shapeBg.graphics.beginFill(0xFFFFFF, 1);
			this.shapeBg.graphics.drawRect(0, 0, 1, 1);
			this.shapeBg.graphics.endFill();
			this.addChild(this.shapeBg);
		}
		
		private function addLogoAnim(): void {
			this.sprLogoAnim = new Bg.classLogoAnim();
			this.sprLogoAnim.x = (this.stage.stageWidth - this.sprLogoAnim.width) / 2;
			this.sprLogoAnim.y = (this.stage.stageHeight - this.sprLogoAnim.height) / 2 - 80;
			this.addChild(sprLogoAnim);
		}
		
		private function onStageResize(e: Event):void {
			this.shapeBg.width = this.stage.stageWidth;
			this.shapeBg.height = this.stage.stageHeight;
		}
		
		private function onRemovedFromStage(e: Event): void {
			this.removeEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
			this.destroy();
		}
		
		public function destroy():void {
			this.stage.removeEventListener(Event.RESIZE, this.onStageResize);
			}
		
	}

}